﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using umbraco.cms.businesslogic.member;
using System.Web.Security;

namespace Zebra.Umbraco.Marcos.MailList
{
    public partial class MailListDetail : LoginRequiredControl
    {

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request["ListID"] != null)
                {
                    using (DataClasses1DataContext db = new DataClasses1DataContext())
                    {
                        surveyMailList mailList = db.surveyMailLists.First(c => c.MailListID == int.Parse(Request["ListID"]) && c.NodeID == int.Parse(Membership.GetUser().ProviderUserKey.ToString()));
                        if (mailList != null)
                        {
                            mailList.surveyMailAddresses.Load();
                            this.lbMailListName.Text = mailList.Name;
                            this.lbLastModifiedOn.Text = mailList.RowModifiedOn.ToString(Resources.Zebra.DateTimeFormat);
                            this.lbMailCount.Text = mailList.surveyMailAddresses.Count.ToString();
                            if (mailList.surveyMailAddresses.Count > 0)
                            {
                                this.GridView1.DataSource = mailList.surveyMailAddresses;
                                this.GridView1.DataBind();
                                GridView1.Visible = true;
                                lbNoRecord.Visible = false;
                            }
                            else
                            {
                                GridView1.Visible = false;
                                lbNoRecord.Visible = true;
                            }
                        }
                    }
                }
            }
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request["MailAddressID"]))
            {
                using (DataClasses1DataContext db = new DataClasses1DataContext())
                {
                    db.ExecuteCommand(string.Format("delete [surveyMailAddress] where [MailAddressID] in ({0})", Request["MailAddressID"]));
                }
            }
            Response.Redirect(string.Format("/MailList/{0}/Detail.aspx", Request["ListID"]));

        }

    }
}